package it.mp.multitask;

import it.mp.multitask.utility.PackageInfoComparator;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;

public class AsyncGetInstalledApps extends AsyncTask<Void, Void, Boolean> implements Serializable {
	private static final long serialVersionUID = -1982375590183344782L;
	private Context ctx;
	private Handler handler;
	private ArrayList<CheckBoxPreference> alCB;
	private PreferenceScreen preferenceScreen;
	public static final int FINISH_OK = 200;
	public static final int FINISH_KO = -200;
	private String PREFISSO = null;
	private OnPreferenceChangeListener onPreferenceChangeListener;
	public AsyncGetInstalledApps(Setup setup, Handler messageHandler, Preference preference) {
		ctx = setup;
		handler = messageHandler;
		preferenceScreen = (PreferenceScreen) preference;
		PREFISSO = "";
	}
	public AsyncGetInstalledApps(Setup setup, Handler messageHandler, Preference preference, String prefisso,
			OnPreferenceChangeListener onPreferenceChangeListener) {
		ctx = setup;
		handler = messageHandler;
		preferenceScreen = (PreferenceScreen) preference;
		PREFISSO = prefisso;
		this.onPreferenceChangeListener = onPreferenceChangeListener;
	}
	protected Boolean doInBackground(Void... arg0) {
		alCB = new ArrayList<CheckBoxPreference>();
		PackageManager manager = ctx.getPackageManager();
		Intent mainIntent = null;

		if (Setup.PREFIX_DONT_SHOW_APPS.equals(PREFISSO)) {
			mainIntent = new Intent();
			final List<PackageInfo> apps = manager.getInstalledPackages(0);
			if (apps != null) {
				Collections.sort(apps, new PackageInfoComparator(manager));
				final int count = apps.size();
				CheckBoxPreference cbPref;
				String packageName = null;
				PackageInfo pi = null;
				for (int index = 0; index < count; index++) {
					pi = apps.get(index);
					packageName = pi.packageName;
					cbPref = new CheckBoxPreference(ctx);
					cbPref.setTitle(manager.getApplicationLabel(pi.applicationInfo));
					cbPref.setKey(PREFISSO + packageName);
					alCB.add(cbPref);
				}

			}
		} else {
			mainIntent = new Intent(Intent.ACTION_MAIN, null);
			mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
			final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
			java.util.Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
			if (apps != null) {
				final int count = apps.size();
				CheckBoxPreference cbPref;
				String packageName = null;
				String className = null;
				for (int index = 0; index < count; index++) {
					ResolveInfo info = apps.get(index);
					packageName = info.activityInfo.applicationInfo.packageName;
					className = info.activityInfo.name;
					cbPref = new CheckBoxPreference(ctx);
					cbPref.setTitle(info.loadLabel(manager));
					if (PREFISSO.equals(Setup.PREFIX_PREFERRED_APPS)) {
						cbPref.setKey(PREFISSO + packageName + Setup.DELIMITER + className);
						cbPref.setOnPreferenceChangeListener(onPreferenceChangeListener);
					} else {
						cbPref.setKey(PREFISSO + packageName);
					}
					alCB.add(cbPref);
				}
			}
		}

		return true;
	}

	protected void onPostExecute(Boolean success) {
		// remove progress dialog if you set one up...
		if (success) {
			Message m = new Message();
			m.what = FINISH_OK;
			m.obj = alCB;
			handler.sendMessage(m);
			preferenceScreen.removeAll();
			for (int index = 0; index < alCB.size(); index++) {
				preferenceScreen.addPreference(alCB.get(index));
			}
		} else {
			Message m = new Message();
			m.what = FINISH_KO;
			handler.sendMessage(m);
		}
	}
	public void setMessageHandler(Handler messageHandler) {
		handler = messageHandler;
	}
}
